﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.IO.Compression;
using System.Runtime.Serialization.Formatters.Binary;
using System.Globalization;
using System.Net.Mail;
using System.ComponentModel;
using System.Text;

namespace Autenticazione.Tools
{
    public class Tools
    {
        public static int GetMaxItemsForPage()
        {
            int nMax = 10; //default

            object nMaxFromConfig = System.Configuration.ConfigurationManager.AppSettings["MaxItemsForPage"];

            if (nMaxFromConfig != null)
            {
                nMax = int.Parse(nMaxFromConfig.ToString());
            }

            return nMax;
        }

        public static string CryptText(string txtToCrypt)
        {
            byte[] key = Convert.FromBase64String("a5GnfX3bZKG=");
            byte[] iv = Convert.FromBase64String("c2NhdG9sYQA=");

            if (!string.IsNullOrEmpty(txtToCrypt))
            {
                DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, dcsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
                StreamWriter sw = new StreamWriter(cs);
                sw.Write(txtToCrypt);
                sw.Flush();
                cs.FlushFinalBlock();
                ms.Flush();

                return Convert.ToBase64String(ms.GetBuffer(), 0, (int)(ms.Length));
            }
            else
                return "";
        }

        public static void FillCombo(System.Web.UI.WebControls.DropDownList ctrl, Object container, string value, string text)
        {
            ctrl.Items.Clear();

            ctrl.DataTextField = text;
            ctrl.DataValueField = value;
            ctrl.DataSource = container;
            ctrl.DataBind();
        }

        public static void FillComboMesi(System.Web.UI.WebControls.DropDownList ctrl)
        {
            System.Web.UI.WebControls.ListItem item;
            ctrl.Items.Clear();

            for (int i = 1; i <= 12; i++)
            {
                item = new System.Web.UI.WebControls.ListItem();
                item.Value = i.ToString();
                item.Text = DateTimeFormatInfo.CurrentInfo.GetMonthName(i);
                ctrl.Items.Add(item);
            }
        }

        public static void FillComboDaysInMonthSelected(System.Web.UI.WebControls.DropDownList ctrl, int month, int year)
        {
            System.Web.UI.WebControls.ListItem item;
            int days;
            string sDay;

            ctrl.Items.Clear();

            days = DateTime.DaysInMonth(year, month);

            for (int i = 1; i <= days; i++)
            {
                sDay = string.Format("0{0}", i.ToString());
                item = new System.Web.UI.WebControls.ListItem(sDay.Substring(sDay.Length - 2), sDay.Substring(sDay.Length - 2));
                ctrl.Items.Add(item);
            }

        }

        public static void FillComboYears(System.Web.UI.WebControls.DropDownList ctrl)
        {
            System.Web.UI.WebControls.ListItem item;
            int minYear = int.Parse(System.Configuration.ConfigurationManager.AppSettings["MinYear"]);
            int maxYear = DateTime.Today.Year;

            for (int i = minYear; i <= maxYear; i++)
            {
                item = new System.Web.UI.WebControls.ListItem(i.ToString(), i.ToString());
                ctrl.Items.Add(item);
            }
        }

        public static void FillComboGender(System.Web.UI.WebControls.DropDownList ctrl)
        {
            System.Web.UI.WebControls.ListItem item;
            ctrl.Items.Clear();

            item = new System.Web.UI.WebControls.ListItem("Maschile", "0");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Feminile", "1");
            ctrl.Items.Add(item);
        }

        public static void FillGrapplingLevel(System.Web.UI.WebControls.DropDownList ctrl)
        {
            System.Web.UI.WebControls.ListItem item;
            ctrl.Items.Clear();

            item = new System.Web.UI.WebControls.ListItem(" ", " ");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("A", "A");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("B", "B");
            ctrl.Items.Add(item);
        }

        public static void MandaMail(string destinationAddr, string user, string password)
        {
            SmtpClient mail = null;
            MailAddress source = null;
            MailAddress destination = null;
            MailMessage message = null;
            string sourceAddr = string.Empty;
            string host = string.Empty;
            string userName = string.Empty;
            string pwd = string.Empty;
            string subject = string.Empty;
            StringBuilder content = new StringBuilder();

            sourceAddr = System.Configuration.ConfigurationManager.AppSettings["From"];
            if (string.IsNullOrEmpty(sourceAddr))
                return;

            host = System.Configuration.ConfigurationManager.AppSettings["Host"];
            if (string.IsNullOrEmpty(host))
                return;

            userName = System.Configuration.ConfigurationManager.AppSettings["UserName"];
            if (string.IsNullOrEmpty(userName))
                return;

            pwd = System.Configuration.ConfigurationManager.AppSettings["Pwd"];
            if (string.IsNullOrEmpty(pwd))
                return;

            subject = System.Configuration.ConfigurationManager.AppSettings["Subject"];
            if (string.IsNullOrEmpty(subject))
                return;

            content.AppendLine(System.Configuration.ConfigurationManager.AppSettings["Content1"]);
            content.AppendLine(System.Configuration.ConfigurationManager.AppSettings["Content2"]);
            content.AppendLine();
            content.AppendLine(System.Configuration.ConfigurationManager.AppSettings["Content3"]);
            content.AppendLine();
            content.AppendLine(System.Configuration.ConfigurationManager.AppSettings["Content4"]);
            content.AppendLine(System.Configuration.ConfigurationManager.AppSettings["Content5"]);
            content.AppendLine();
            content.AppendLine(System.Configuration.ConfigurationManager.AppSettings["Content6"]);
            content.AppendLine(string.Format("Username: {0}", user));
            content.AppendLine(string.Format("Password: {0}", password));
            mail = new SmtpClient();

            mail.Host = host;
            System.Net.NetworkCredential nc = new System.Net.NetworkCredential();
            nc.UserName = userName;
            nc.Password = pwd;
            mail.Credentials = nc;
            
            try
            {
                source = new MailAddress(sourceAddr);
                destination = new MailAddress(destinationAddr);
                message = new MailMessage(source, destination);
                message.Body = content.ToString();
                message.Subject = subject;
                mail.Send(message);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.Write(e.Message);
            }
            finally
            {
                message.Dispose();
            }
            
        }

        public static void FillFights(System.Web.UI.WebControls.DropDownList ctrl)
        {
            System.Web.UI.WebControls.ListItem item;
            ctrl.Items.Clear();

            item = new System.Web.UI.WebControls.ListItem("Nessuna selezione", "-1");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Tutti", "0");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Chauss Fight Assalto", "1");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Savate Assalto", "2");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Point Fight", "3");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Light Contact", "4");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("KickLight", "5");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("K1 Light", "6");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Thai Light", "7");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("MMA Light", "8");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Grappling", "9");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Chauss Fight Combact", "10");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Savate Combact", "11");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Full Contact", "12");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Low Kick", "13");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("K1 Rules", "14");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("Thai Full Rules", "15");
            ctrl.Items.Add(item);

            item = new System.Web.UI.WebControls.ListItem("MMA Full Rules", "16");
            ctrl.Items.Add(item);
        }

    }
}